﻿using Microsoft.Build.Construction;
using Microsoft.Configuration;
using Microsoft.Extensions.Configuration;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Xejen.NugetServer.Tools
{
    public partial class MainForm : Form
    {
        private readonly IConfiguration _configuration;
        private readonly NugetConfig _nugetConfig;
        public MainForm(IConfiguration configuration)
        {
            _configuration = configuration;

            NugetConfig config = new NugetConfig();
            _configuration.Bind(nameof(NugetConfig), config);
            _nugetConfig = config;

            InitializeComponent();
        }

        /// <inheritdoc/>
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            SolutionPathComboBox.SelectedIndex = 0;
        }

        /// <summary>
        /// 复制并打开
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            string solutionPath = SolutionPathComboBox.Text.Trim();
            if (string.IsNullOrWhiteSpace(solutionPath))
            {
                MessageBox.Show("请选择解决方案", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            SolutionFile solution = SolutionFile.Parse(solutionPath);
            IEnumerable<ProjectInSolution> projectsInSolution = solution.ProjectsInOrder.Where(p => p.ProjectType != SolutionProjectType.SolutionFolder);
            List<string> debugs = projectsInSolution.Select(q =>
            {
                var absolutePath = q.AbsolutePath;
                var absolutePathItems = absolutePath.Split('\\').ToList();
                absolutePathItems.RemoveAt(absolutePathItems.Count - 1);
                absolutePathItems.Add("bin");
                absolutePathItems.Add("debug");

                var debug = string.Join("\\", absolutePathItems.ToArray());

                return debug;
            }).ToList();

            List<FileInfo> nugets = new List<FileInfo>();

            foreach (var debug in debugs)
            {
                if (!Directory.Exists(debug))
                {
                    continue;
                }

                FileInfo[] nugetFiles = new DirectoryInfo(debug).GetFiles("*.nupkg");
                if (nugetFiles.Any())
                {
                    var latestNugetFile = nugetFiles.OrderByDescending(file => file.LastWriteTime).First();
                    nugets.Add(latestNugetFile);
                }
            }

            string targetFolder = TargetPathTextBox.Text.Trim();
            if (!Directory.Exists(targetFolder))
            {
                Directory.CreateDirectory(targetFolder);
            }
            foreach (FileInfo nugetFileInfo in nugets)
            {
                File.Copy(nugetFileInfo.FullName, Path.Combine(targetFolder, nugetFileInfo.Name), true);
            }

            _configuration.Set(nameof(NugetConfig), _nugetConfig);

            System.Diagnostics.Process.Start("Explorer.exe", targetFolder);
        }
    }
}
